package com.example.ll_video.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ll_video.bean.SubjectInfo;
import com.example.ll_video.bean.SubjectType;
import com.example.ll_video.constant.Constant;
import com.example.ll_video.utils.ImageLoadUtils;
import com.example.ll_video.R;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SubjectInfoAdapter extends RecyclerView.Adapter<SubjectInfoAdapter.ViewHolder> {


    private Context context;
    private ItemClickListener itemClickListener;
    private List<SubjectInfo> myData;

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public SubjectInfoAdapter(Context context, List<SubjectInfo> myData) {
        this.context = context;
        this.myData = myData;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.subjectintfo_adapter_math, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SubjectInfo subjectInfo = myData.get(position);
        String imageUrl = subjectInfo.getImage();
        if (!imageUrl.contains("http")){
            imageUrl = Constant.HOST + imageUrl;
        }
        ImageLoadUtils.imageLoadUrl(imageUrl,holder.ivSubjectCover);
        holder.tvSubjectTitle.setText(subjectInfo.getTitle());
    }

    @Override
    public int getItemCount() {
        return myData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_subject_cover)
        QMUIRadiusImageView ivSubjectCover;
        @BindView(R.id.tv_subject_title)
        TextView tvSubjectTitle;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public interface ItemClickListener {
        void onItemClick(int position, SubjectType subjectType);
    }

}
